// 存储到vuex的同时存储到本地存储
import { getItem, setItem } from '@/utils/auth.js'
// 获取个人信息
import { userProfile } from '@/api/user.js'
const state = {
  token: getItem() || '',
  userInfo: ''
}
const mutations = {
  getState (state, value) {
    state.token = value
    setItem(value)
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  //   修改个人信息
  editUserInfo (state, value) {
    // value:[key,value]
    // state.userInfo[value[0]] = value[1]
    // value:{key:'属性名',value：值}
    state.userInfo[value.key] = value.value
  }
}
const actions = {
  async getUserInfo (store) {
    if (store.state.userInfo === '') {
      const res = await userProfile()
      store.commit('setUserInfo', res.data)
      //  console.log(res)
    }
  }
}
const getters = {
  getToken (state) {
    return state.token
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
